//
//  WeatherAnimation.m
//  CC_Weather
//
//  Created by rimi on 16/10/10.
//  Copyright © 2016年 cc. All rights reserved.
//

#import "WeatherAnimation.h"

@implementation WeatherAnimation


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

//设置粒子发射器图层
- (void)setup {
    //粒子发射器图层
    _emitterLayer=[CAEmitterLayer layer];
    
    //粒子发射器位置
    _emitterLayer.emitterPosition=CGPointMake(self.bounds.size.width * 0.5, -100);
    
    //粒子发射器的范围
    _emitterLayer.emitterSize= CGSizeMake(self.bounds.size.width, 0);
    
    
    //发射模式
    _emitterLayer.emitterMode=kCAEmitterLayerOutline;
    
    //发射形状
    _emitterLayer.emitterShape=kCAEmitterLayerLine;
    
    [self.layer addSublayer:_emitterLayer];
    
    //创建粒子
    _emitterCell=[CAEmitterCell emitterCell];
    
    
    //云
    _cloudyImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(self.bounds.size.width, self.bounds.size.height * 0.15, self.bounds.size.width * 0.6, self.bounds.size.height * 0.15)];
    _cloudyImageView1.image = [UIImage imageNamed:@"wuyun"];
    _cloudyImageView1.alpha = 0.5;
    
    _cloudyImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(-300, self.bounds.size.height * 0.1, self.bounds.size.width * 0.6, self.bounds.size.height * 0.15)];
    _cloudyImageView2.image = [UIImage imageNamed:@"wuyun"];
    _cloudyImageView2.alpha = 0.5;

    
    _cloudyImageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(self.bounds.size.width * 0.1, self.bounds.size.height * 0.05, self.bounds.size.width * 0.6, self.bounds.size.height * 0.15)];
    _cloudyImageView3.image = [UIImage imageNamed:@"wuyun"];
    _cloudyImageView3.alpha = 0.5;
    
    
    _cloudyImageView4 = [[UIImageView alloc]initWithFrame:CGRectMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.13, self.bounds.size.width * 0.6, self.bounds.size.height * 0.15)];
    _cloudyImageView4.image = [UIImage imageNamed:@"wuyun"];
    _cloudyImageView4.alpha = 0.5;
    
    //太阳
    _sun = [[UIImageView alloc]initWithFrame:CGRectMake(self.bounds.size.width * 0.62, self.bounds.size.height * 0.076, 100, 100)];
    _sun.image = [UIImage imageNamed:@"rotationSun"];
    //旋转
    [self rotation];
    
}


#pragma mark - 下雨
//初始化雨
- (void)showRain{
    
    //滞空layer层
    _emitterLayer.emitterCells = nil;
    //移除子视图
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    //停止所有动画
    _cloudyImageView1.transform = CGAffineTransformIdentity;
    _cloudyImageView2.transform = CGAffineTransformIdentity;
    _cloudyImageView3.transform = CGAffineTransformIdentity;
    _cloudyImageView4.transform = CGAffineTransformIdentity;
    _sun.transform = CGAffineTransformIdentity;
    
    //设置粒子内容
    _emitterCell.contents=(__bridge id)([UIImage imageNamed:@"raindrop"].CGImage);
    
    //设置粒子缩放比例
    _emitterCell.scale=0.3;
    
    //缩放范围
    _emitterCell.scaleRange=0.2;
    
    //每秒粒子产生数量
    _emitterCell.birthRate=60;
    
    //粒子生命周期
    _emitterCell.lifetime=3;
    
    //粒子透明速度
    _emitterCell.alphaSpeed=-0.5;
    
    //粒子速度
    _emitterCell.velocity=200;
    _emitterCell.velocityRange=100;
    
    //设置发射角度
    _emitterCell.emissionLongitude=M_PI;
    //emitterCell.emissionRange=M_PI;
    
    //y轴加速度
    //emitterCell.yAcceleration = 2;
    
    
    //设置粒子旋转角速度
    //emitterCell.spin=M_PI_4;
    
    //设置layer的粒子
    _emitterLayer.emitterCells=@[_emitterCell];
    
    //_rainDropEmitterLayer.transform=CATransform3DMakeRotation(-M_PI/4, 0, 0, 1);
    
}


#pragma mark - 云
//初始化云
- (void)showCloudy{
    
    //layer层滞空
     _emitterLayer.emitterCells=nil;
    //移除子视图
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    //停止所有动画
    _cloudyImageView1.transform = CGAffineTransformIdentity;
    _cloudyImageView2.transform = CGAffineTransformIdentity;
    _cloudyImageView3.transform = CGAffineTransformIdentity;
    _cloudyImageView4.transform = CGAffineTransformIdentity;
    _sun.transform = CGAffineTransformIdentity;
    
    
    
    //云
    
    [self addSubview:_cloudyImageView1];
    
    [self addSubview:_cloudyImageView2];

    [self addSubview:_cloudyImageView3];
    
    [self addSubview:_cloudyImageView4];
    
    [self animation1];
    [self animation2];
}

- (void)animation1{
    [UIView animateWithDuration:20 animations:^{
        _cloudyImageView1.frame = CGRectMake(-300, self.bounds.size.height * 0.15, self.bounds.size.width * 0.6, self.bounds.size.height * 0.15);
        _cloudyImageView1.alpha = 0.2;
        
        _cloudyImageView2.frame = CGRectMake(self.bounds.size.width +200, self.bounds.size.height * 0.1, self.bounds.size.width * 0.6, self.bounds.size.height * 0.15);
        _cloudyImageView2.alpha = 0.2;
        
    } completion:^(BOOL finished) {
        
        _cloudyImageView1.frame = CGRectMake(self.bounds.size.width, self.bounds.size.height * 0.15, self.bounds.size.width * 0.6, self.bounds.size.height * 0.15);
        _cloudyImageView1.alpha = 0.5;
        _cloudyImageView2.frame = CGRectMake(-300, self.bounds.size.height * 0.1, self.bounds.size.width * 0.6, self.bounds.size.height * 0.15);
        _cloudyImageView2.alpha = 0.5;
        [self animation1];
    }];
}

- (void)animation2{
    [UIView animateWithDuration:15 animations:^{
        
        _cloudyImageView3.frame = CGRectMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.05, self.bounds.size.width * 0.6, self.bounds.size.height * 0.15);
        _cloudyImageView4.frame = CGRectMake(self.bounds.size.width * 0.1, self.bounds.size.height * 0.13, self.bounds.size.width * 0.6, self.bounds.size.height * 0.15);
    } completion:^(BOOL finished) {
        [self animation3];
    }];
}

- (void)animation3{
    
    [UIView animateWithDuration:15 animations:^{
        _cloudyImageView3.frame = CGRectMake(self.bounds.size.width * 0.1, self.bounds.size.height * 0.05, self.bounds.size.width * 0.6, self.bounds.size.height * 0.15);
        _cloudyImageView4.frame = CGRectMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.13, self.bounds.size.width * 0.6, self.bounds.size.height * 0.15);
    } completion:^(BOOL finished) {
        [self animation2];
    }];
}

#pragma mark - 雪
//初始化雪
- (void)showSnow{
    
    //layer层滞空
    _emitterLayer.emitterCells=nil;
    //移除子视图
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    //停止所有动画
    _cloudyImageView1.transform = CGAffineTransformIdentity;
    _cloudyImageView2.transform = CGAffineTransformIdentity;
    _cloudyImageView3.transform = CGAffineTransformIdentity;
    _cloudyImageView4.transform = CGAffineTransformIdentity;
    _sun.transform = CGAffineTransformIdentity;
    
    //设置粒子内容
    _emitterCell.contents=(__bridge id)([UIImage imageNamed:@"snow"].CGImage);
    
    //设置粒子缩放比例
    _emitterCell.scale=0.2;
    
    //缩放范围
    _emitterCell.scaleRange=0.1;
    
    //每秒粒子产生数量
    _emitterCell.birthRate=30;
    
    //粒子生命周期
    _emitterCell.lifetime=4;
    
    //粒子透明速度
    _emitterCell.alphaSpeed=-0.2;
    
    //粒子速度
    _emitterCell.velocity=100;
    _emitterCell.velocityRange=100;
    
    //设置发射角度
    _emitterCell.emissionLongitude=M_PI;
    _emitterCell.emissionRange=M_PI_2;
    
    //设置粒子旋转角速度
    _emitterCell.spin=M_PI_4;
    
    //设置layer的粒子
    _emitterLayer.emitterCells=@[_emitterCell];
    
    //_emitterLayer.transform=CATransform3DMakeRotation(-M_PI/6, 0, 0, 1);
}

#pragma mark - 太阳
//初始化太阳
- (void)showSun{
    
    //layer层滞空
    _emitterLayer.emitterCells=nil;
    //移除子视图
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    //停止所有动画
    _cloudyImageView1.transform = CGAffineTransformIdentity;
    _cloudyImageView2.transform = CGAffineTransformIdentity;
    _cloudyImageView3.transform = CGAffineTransformIdentity;
    _cloudyImageView4.transform = CGAffineTransformIdentity;
    _sun.transform = CGAffineTransformIdentity;
    
    //太阳
    [self addSubview:_sun];
    
}

//旋转
- (void)rotation{
    
    [UIView animateWithDuration:3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        _sun.transform = CGAffineTransformRotate(_sun.transform, M_PI_2);
    } completion:^(BOOL finished) {
        
        [self rotation];
        
    }];
    
}

@end
